<?php if (!defined('THINK_PATH')) exit();?><script type="text/javascript">
<!--
var total_amount = $("input[name=total_amount]");
var trade_amount = $("input[name=trade_amount]");
var odd_amount = $("input[name=odd_amount]");
var total_nums = $("input[name=total_nums]");
$(function(){
    
    // 初始化應收金額和實收金額
	total_amount.val((0).toFixed(2));
	trade_amount.val((0).toFixed(2));
    total_nums.val(0);
	
	// 扣除零頭
	odd_amount.keyup(function(){
		trade_amount.val((total_amount.val() - $(this).val()).toFixed(2));
	});
});

function calculate(t, del){
    var tbody = $(t).parents('tbody');
    var total = 0;
    var totalnums = 0;
    var fix = 0; 
    var fixnums=0;// 刪除的話應該忽略本行
    if (del == 1){
        //alert(0);
        $(t).parents("tr:first").each(function(){
            var orgCount = 0;
            var orgPrice = 0;
            var orgDiscount = 1;
            var orgTotal = 0;
            $(this).find('input').each(function(){
                if (this.name == 'items.orgCount[]') orgCount = this.value == '' ? 0 : this.value;
                if (this.name == 'items.orgPrice[]') orgPrice = this.value == '' ? 0 : this.value;
                if (this.name == 'items.orgDiscount[]') orgDiscount = this.value == '' ? 1 : (1 - this.value/100);
                if (this.name == 'items.orgTotal[]'){
                    orgTotal = orgCount*orgPrice*orgDiscount;
                    if (orgTotal == 0){
                        this.value = '';
                    } else {
                        this.value = orgTotal.toFixed(2);
                    }
                }
            });
            fix = orgTotal;
            fixnums =orgCount;
        });
    }
    $(tbody).find('tr').each(function(){
        var orgCount = 0;
        var orgPrice = 0;
        var orgDiscount = 1;
        var orgTotal = 0;
        $(this).find('input').each(function(){
            if (this.name == 'items.orgCount[]') orgCount = this.value == '' ? 0 : 1*this.value;
            if (this.name == 'items.orgPrice[]') orgPrice = this.value == '' ? 0 : this.value;
            if (this.name == 'items.orgDiscount[]') orgDiscount = this.value == '' ? 1 : (1 - this.value/100);
            if (this.name == 'items.orgTotal[]'){
                orgTotal = orgCount*orgPrice*orgDiscount;
                if (orgTotal == 0){
                    this.value = '';
                } else {
                    this.value = orgTotal.toFixed(2);
                }
            }
        });
        total += orgTotal;
        totalnums +=orgCount;
    });
    if (fix != 0){
        total -= fix;
        totalnums -= fixnums;
    }
    total_nums.val(totalnums);
    total_amount.val(total.toFixed(2));
    trade_amount.val((total - odd_amount.val()).toFixed(2));
}
//-->
</script>
<h2 class="contentTitle">进货单(JH)</h2>
<form class="pageForm required-validate"  action="__URL__/addOrders/navTabId/__MODULE__" method="post" onsubmit="return validateCallback(this, dialogAjaxDone)">
<div class="pageContent">
	<div class="pageFormContent" layoutH="97">
    <fieldset>
        <table style="white-space: nowrap;">
        <tr>
          <td>单据编号：</td>
          <td><input class="required" name="ordernumber" readonly="true" size="30" type="text" value="<?php echo ($newCode); ?>" /></td>
          <td>单据日期：</td>
          <td><input class="date required" name="ordertime" type="text" value="<?php echo ($dtime); ?>" /><a class="inputDateButton" href="javascript:;">选择</a></td>
         </tr>
         <tr>
          <td>供货单位：</td>
          <td>
          <input id="sendName" name="sendName.id" value="" type="hidden"/>
          <input class="required" name="sendName.orgName" type="text" postField="keyword" suggestFields="id"  size="50" readonly="true" />
	      <a class="btnLook" href="__APP__/Public/ckBackContacts/tpy/2" lookupGroup="sendName" width="1000" height="450">查找带回</a>          
          </td>
          <td>经&nbsp;&nbsp;办&nbsp;&nbsp;人：</td>
          <td>
          <input id="orderUser" name="orderUser.id" value="" type="hidden"/>
          <input class="required" name="orderUser.orgName" type="text" postField="keyword" suggestFields="id"  readonly="true" />
	      <a class="btnLook" href="__APP__/Public/ckBackUser/" lookupGroup="orderUser">查找带回</a>          
          </td>
         </tr>
         <tr>
          <td>收货仓库：</td>
          <td>
          <input id="warehouse" name="warehouse.id" value="" type="hidden"/>
          <input class="required" name="warehouse.orgName" type="text" postField="keyword" suggestFields="id" size="30" readonly="true" />
	      <a class="btnLook" href="__APP__/Public/ckBackWarehouse/" lookupGroup="warehouse">查找带回</a>          
          </td>
          <td>發票號碼：</td>
          <td><input name="invoices" size="30" type="text" /></td>
         </tr>
         <tr>
          <td>单据摘要：</td>
          <td colspan="3"><input name="remarks" size="120" type="text" /></td>
         </tr>
        </table>
    </fieldset>
    <fieldset>
<table class="list nowrap itemDetail" addButton="添加商品行數" width="100%">
<thead>
	<tr>
        <th type="text" name="items.proid[]" fieldClass="readonly" readOnly size="2">編 號</th>
		<th type="lookup" name="items.partnumber[]" lookupGroup="items" lookupUrl="__APP__/Public/ckBackProducts/flag/JH" suggestFields="partnumber" size="12" fieldClass="required">料 号</th>
		<th type="text" name="items.prodname[]" fieldClass="readonly" size="22" readOnly>名 称</th>
		<th type="text" name="items.prodsize[]" fieldClass="readonly" readOnly>規 格</th>
        <th type="text" name="items.partmodel[]" fieldClass="readonly" readOnly>型 号</th>
        <th type="text" name="items.pmaterial[]" fieldClass="readonly" readOnly>材 质</th>
		<th type="text" name="items.unit[]" fieldClass="readonly" readOnly size="2">單 位</th>
		<th type="calculate" name="items.orgCount[]" fieldClass="required" size="4">數 量</th>
		<th type="calculate" name="items.orgPrice[]" size="5">单 价(￥)</th>
		<th type="calculate" name="items.orgDiscount[]" size="2">折 扣(%)</th>
		<th type="text" name="items.orgTotal[]" size="8" readOnly>金 额(￥)</th>
        <th type="text" name="items.orgMark[]" size="15">备 注</th>
		<th onclick="calculate(this,1)" type="del" fieldClass="refreshAmount">删行</th>
	</tr>
</thead>
<tbody></tbody>
</table>
    </fieldset>
    </div>
	<div class="formBar">
    <label style="float:left">累计数量:<input name="total_nums" size="3" type="text" readonly="readonly" /></label>
    <label style="float:left">&nbsp;&nbsp;折扣金额:<input name="odd_amount" size="5" type="text" /></label>
    <label style="float:left">&nbsp;&nbsp;应付金额:<input name="total_amount" size="8" type="text" readonly="readonly" /></label>
    <label style="float:left">&nbsp;&nbsp;实付金额:<input name="trade_amount" size="8" type="text" /></label>
    <label style="float:left">&nbsp;&nbsp;制单人:<input name="createMan" value="<?php echo ($_SESSION['loginUserName']); ?>" size="10" type="text" readonly="true" /></label>    
	<label style="float: left;">&nbsp;&nbsp;入库:<input type="checkbox" id="goStock" name="goStock" value="1" checked="checked" /></label>
    <label style="float: left;">&nbsp;&nbsp;&nbsp;付款:<input type="checkbox" id="goFunds" name="goFunds" value="1" checked="checked" /></label>
    <label style="float:left">&nbsp;&nbsp;&nbsp;付款账户:
    <SELECT name="bankid">
     <?php if(is_array($listBank)): $i = 0; $__LIST__ = $listBank;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vu): $mod = ($i % 2 );++$i;?><option value="<?php echo ($vu["id"]); ?>"><?php echo ($vu["bankname"]); ?></option><?php endforeach; endif; else: echo "" ;endif; ?>
	</SELECT>
    </label>
    <ul>
		<li><div class="buttonActive"><div class="buttonContent"><button type="submit">提交单据</button></div></div></li>
		<li><div class="button"><div class="buttonContent"><button class="close" type="button">关闭退出</button></div></div></li>
	</ul>
	</div>
</div>
</form>